using System;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.InputSystem;
public class PlayerInput : SingletonMonoBehaviour<PlayerInput>
{
    // 输入系统脚本
    private PlayerInputSystem playerInputSystem;
    
    /****************** 移动 *******************/
    public bool GetIsInputMove => inputAxes != Vector2.zero;                                          // 是否移动
    private Vector2 inputAxes => playerInputSystem.GamePlay.Move.ReadValue<Vector2>();                // 移动输入
    
    // X轴 和 Y轴 均取整数
    public Vector2 GetInputAxes
    {
        get
        {
            // 没有输入返回 0
            if (inputAxes == Vector2.zero)
            {
                return Vector2.zero;
            }
            // X轴有输入，Y轴无输入
            else if (inputAxes.x != 0f && inputAxes.y == 0f)
            {
                return new Vector2(inputAxes.x > 0f ? 1.0f : -1.0f, 0f);
            }
            // X轴无输入，Y轴有输入
            else if (inputAxes.x == 0f && inputAxes.y != 0f)
            {
                return new Vector2(0f, inputAxes.y > 0f ? 1.0f : -1.0f);
            }
            // X轴，Y轴都有输入
            else
            {
                return new Vector2(inputAxes.x > 0f ? 1.0f : -1.0f, inputAxes.y > 0f ? 1.0f : -1.0f);
            }
        }
    }
    /****************** 鼠标状态 *******************/
    public Vector3 MouseWorldPosition => Camera.main.ScreenToWorldPoint(new Vector3(Mouse.current.position.ReadValue().x, Mouse.current.position.ReadValue().y, 0));
    private int mouseStateIndx = 0;    // 0 Normal, 1 A Star, 2 Use Tool/Item
    public MouseState MouseState { get;private set; } = MouseState.Normal;
    private bool isSwitchMouseState => playerInputSystem.GamePlay.SwitchMouseState.WasPressedThisFrame();     // 切换鼠标状态
    
    /****************** 工具按键 *******************/
    public bool GetIsUseTool => playerInputSystem.GamePlay.UseTool.WasPressedThisFrame();                     // 使用工具                                                                         // 使用工具
    public bool GetIsToolkit1 => playerInputSystem.GamePlay.Toolkit1.WasPressedThisFrame();                   // 攻击
    public bool GetIsToolkit2 => playerInputSystem.GamePlay.Toolkit2.WasPressedThisFrame();                   // 斧头
    public bool GetIsToolkit3 => playerInputSystem.GamePlay.Toolkit3.WasPressedThisFrame();                   // 锄头
    public bool GetIsToolkit4 => playerInputSystem.GamePlay.Toolkit4.WasPressedThisFrame();                   // 鹤嘴锄
    public bool GetIsToolkit5 => playerInputSystem.GamePlay.Toolkit5.WasPressedThisFrame();                   // 洒水
    public bool GetIsToolkit6 => playerInputSystem.GamePlay.Toolkit6.WasPressedThisFrame();                   // 物品栏1
    public bool GetIsToolkit7 => playerInputSystem.GamePlay.Toolkit7.WasPressedThisFrame();                   // 物品栏2
    public bool GetIsToolkit8 => playerInputSystem.GamePlay.Toolkit8.WasPressedThisFrame();                   // 物品栏3
    public bool GetIsToolkit9 => playerInputSystem.GamePlay.Toolkit9.WasPressedThisFrame();                   // 物品栏4
    public bool GetIsToolkit10 => playerInputSystem.GamePlay.Toolkit10.WasPressedThisFrame();                 // 物品栏5
    
    /****************** 场景&物品交互 *******************/
    public bool GetIsInteraction => playerInputSystem.GamePlay.Interaction.WasPressedThisFrame();             // 交互，物品、人物 
    public bool GetIsPickUp => playerInputSystem.GamePlay.PickUp.WasPressedThisFrame();                       // 拾取物品
    private Vector2 GetInputDerectionSwitch => playerInputSystem.GamePlay.Switch.ReadValue<Vector2>();        // 方向输入切换
    
    /****************** 设置玩家朝向 *******************/
    public  float Horizontal { get;private set; }
    public float Vertical{ get;private set; }

    protected override void Awake()
    {
        base.Awake();
        playerInputSystem = new PlayerInputSystem();
        
        // 初始化朝向数据
        Horizontal = 1;
        Vertical = 1;
    }
    
    private void OnEnable()
    {
        // 激活输入系统
        playerInputSystem.Enable();
    }

    private void Update()
    {
        MouseStateSetting();
        PlayerFaceSettingVertical();
        PlayerFaceSettingHorizontal();
    }

    private void OnDisable()
    {
        // 取消激活输入系统
        playerInputSystem.Disable();
    }

    private void MouseStateSetting()
    {
        if (isSwitchMouseState)
        {
            mouseStateIndx = (mouseStateIndx += 1) % 3;
            switch (mouseStateIndx)
            {
                case 0: MouseState = MouseState.Normal;
                    break;
                case 1: MouseState = MouseState.AStar;
                    break;
                case 2: MouseState = MouseState.UseToolOrItem;
                    break;
            }
        }
    }
    
    private void PlayerFaceSettingVertical()
    {
        // 移动设计
        // Y轴没有输入并且X轴有输入，设置为侧向状态
        if (GetInputAxes.x != 0 && GetInputAxes.y == 0)
        {
            Vertical = 0;
        }
        // Y轴有输入，按照Y轴输入设置UpDown，正面 1 ，背面 0.5
        else
        {
            if (GetInputAxes.y < 0)
            {
                Vertical = 1;
            }
            else if (GetInputAxes.y > 0)
            {
                Vertical = 0.5f;
            }
        }
    }
    
    private void PlayerFaceSettingHorizontal()
    {
        // 按照X轴输入设置朝向，即 Scale.X ，向左向右
        if (GetInputAxes.x > 0)
        {
            Horizontal = 1;
        }
        else if (GetInputAxes.x < 0)
        {
            Horizontal = -1;
        }
    }
}
